﻿
using DataAccess;
using Magazijn_Layer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Assemblage_lib
{

    public class Assemblage : I_Assemblage
    {
        Database database = new Database();
        Magazijn magazijn = new Magazijn();
        public Assemblage()
        {
            
        }

        public void Assembleer(string update, int orderID)
        {
            DataTable status = database.Select("SELECT Id FROM Status WHERE Omschrijving = '" + update + "'");
            int message = database.Update("UPDATE bestelling SET statusId =" + status.Rows[0][0] + " WHERE Id =" + orderID);

            DataTable product = database.Select("SELECT Id FROM Product WHERE Id IN (SELECT ProductId FROM ProductOrder WHERE OrderId = " + id + ")");
            DataTable aantal = database.Select("SELECT aantal FROM ProductOrder WHERE OrderId = " + id);

            foreach (DataRow pId in product.Rows)
            {
                DataTable result = magazijn.Zoek(Convert.ToInt32(pId["Id"]));
                if (Convert.ToInt32(result.Rows[0][4]) >= Convert.ToInt32(aantal.Rows[0][0]))
                {
                    magazijn.Verwijderen(Convert.ToInt32(pId[0]), Convert.ToInt32(aantal.Rows[0][0]));
                }
            }
            
            if (message != 0)
            {
                MessageBox.Show("Order is bijgewerkt");
            }
            else
            {
                MessageBox.Show("Er is een fout opgetreden.\n\rOrder is NIET bijgewerkt!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BewerkInventaris() 
        { }
    }
}
